package com.eascs.ticket.ticket.controller;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.common.assertion.util.AssertInfoUtils;
import com.eascs.common.util.ObjectUtil;
import com.eascs.common.util.StringUtil;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.ticket.common.controller.BaseActionRestController;
import com.eascs.ticket.common.entity.EntityUtil;
import com.eascs.ticket.configuration.entity.ProjectCommentTemplate;
import com.eascs.ticket.configuration.entity.TicketTypeStatus;
import com.eascs.ticket.configuration.service.ProjectCommentTemplateService;
import com.eascs.ticket.configuration.service.TicketTypeStatusService;
import com.eascs.ticket.project.service.ProjectModuleService;
import com.eascs.ticket.project.service.ProjectTicketTypeService;
import com.eascs.ticket.project.service.ProjectVersionService;
import com.eascs.ticket.ticket.entity.Ticket;
import com.eascs.ticket.ticket.entity.TicketDescriptionsUpdate;
import com.eascs.ticket.ticket.entity.TicketPlanUpdate;
import com.eascs.ticket.ticket.entity.TicketUpdate;
import com.eascs.ticket.ticket.form.ChangeOperatorForm;
import com.eascs.ticket.ticket.form.ChangeStatusForm;
import com.eascs.ticket.ticket.form.GetAssignViewForm;
import com.eascs.ticket.ticket.form.GetChangeStatusViewForm;
import com.eascs.ticket.ticket.form.GetProjectVersionEditHtmlForm;
import com.eascs.ticket.ticket.form.GetTicketEditHtmlForm;
import com.eascs.ticket.ticket.form.GetTicketTypeHtmlForm;
import com.eascs.ticket.ticket.form.GetTicketTypeTemplateForm;
import com.eascs.ticket.ticket.form.InsertTicketForm;
import com.eascs.ticket.ticket.form.TicketDescriptionsUpdateForm;
import com.eascs.ticket.ticket.form.TicketIdForm;
import com.eascs.ticket.ticket.form.UpdateTicketForm;
import com.eascs.ticket.ticket.form.UpdateTicketPlanForm;
import com.eascs.ticket.ticket.service.TicketService;
import com.eascs.ticket.ticket.vo.TicketChangeStatusVo;
import com.eascs.ticket.ticket.vo.TicketUpdateOperatorVo;
import com.eascs.ticket.user.form.AutocompleteForm;
import com.eascs.web.sso.UserHolder;

@RestController
public class TicketRestController extends BaseActionRestController {

	@Inject
	private ProjectModuleService projectModuleService;

	@Inject
	private ProjectTicketTypeService projectTicketTypeService;

	@Inject
	private ProjectVersionService projectVersionService;

	@Inject
	private TicketService ticketService;

	@Inject
	private InsertTicketComponet insertTicketComponet;

	@Inject
	private TicketTypeStatusService ticketTypeStatusService;

	@Inject
	private ProjectCommentTemplateService projectCommentTemplateService;

	@RequestMapping(value = "/ticket/getTicketTypeHtml")
	public HtmlRestResult getTicketTypeHtml(GetTicketTypeHtmlForm getTicketTypeHtmlForm) {
		String projectId = getTicketTypeHtmlForm.getProjectId();
		if (StringUtil.hasText(projectId)) {
			ViewContext.put("projectId", projectId);
			ViewContext.put("ticketTypeDataSouce", projectTicketTypeService.getMainTicketTypeDataSouceByProjectId(projectId));
		}
		return HtmlRestResult.html("ticket/getTicketTypeHtml");
	}

	@RequestMapping(value = "/ticket/getTicketTypeTemplate")
	public HtmlRestResult getTicketTypeTemplate(GetTicketTypeTemplateForm getTicketTypeTemplateForm) {
		String projectId = getTicketTypeTemplateForm.getProjectId();
		String ticketTypeCode = getTicketTypeTemplateForm.getTicketTypeCode();
		String template = "ticket/ticketTypeTemplate/edit/default";
		if (StringUtil.hasText(projectId) && StringUtil.hasText(ticketTypeCode)) {
			ViewContext.put("projectId", projectId);
			ViewContext.put("ticketTypeCode", ticketTypeCode);
			template = "ticket/ticketTypeTemplate/edit/" + ticketTypeCode;
			ViewContext.put("moduleDataSouce", projectModuleService.getProjectModuleDataSouceByProjectId(projectId));
			ViewContext.put("validProjectVersionDataSouce", projectVersionService.getValidProjectVersionByProjectId(projectId));
		}
		return HtmlRestResult.html(template);
	}

	@RequestMapping(value = "/ticket/getTicketEditHtml")
	public HtmlRestResult getTicketEditHtml(GetTicketEditHtmlForm getTicketEditHtmlForm) {
		Ticket ticket = this.ticketService.getTicketById(getTicketEditHtmlForm.getTicketId());
		ViewContext.put("ticket", ticket);
		ViewContext.put("moduleDataSouce", projectModuleService.getProjectModuleDataSouceByProjectId(ticket.getProjectId()));
		return HtmlRestResult.popHtmlFragment("ticket/getTicketEditHtml");
	}

	@RequestMapping(value = "/ticket/getUpdatePlanHtml")
	public HtmlRestResult getUpdatePlanHtml(TicketIdForm ticketIdForm) {
		Ticket ticket = this.ticketService.getTicketById(ticketIdForm.getTicketId());
		ViewContext.put("ticket", ticket);
		ViewContext.put("validProjectVersionDataSouce", projectVersionService.getValidProjectVersionByProjectId(ticket.getProjectId()));
		return HtmlRestResult.popHtmlFragment("ticket/getUpdatePlanHtml");
	}

	@RequestMapping(value = "/ticket/getProjectVersionEditHtml")
	public HtmlRestResult getProjectVersionEditHtml(GetProjectVersionEditHtmlForm getProjectVersionEditHtmlForm) {
		Ticket ticket = null;
		if (StringUtil.hasText(getProjectVersionEditHtmlForm.getTicketId())) {
			ticket = this.ticketService.getTicketById(getProjectVersionEditHtmlForm.getTicketId());
		} else {
			ticket = new Ticket();
			ticket.setProjectId(getProjectVersionEditHtmlForm.getProjectId());
		}
		ViewContext.put("ticket", ticket);
		return new HtmlRestResult("ticket/getProjectVersionEditHtml");
	}

	@RequestMapping(value = "/ticket/insert")
	public BaseRestResult<String> insertTicket(InsertTicketForm insertTicketForm) {
		Ticket ticket = insertTicketComponet.insertTicket(insertTicketForm);
		return BaseRestResult.redirect("/ticket/view/" + ticket.getId());
	}

	@RequestMapping(value = "/ticket/update")
	public BaseRestResult<String> updateTicket(UpdateTicketForm updateTicketForm) {
		insertTicketComponet.checkStartAndEndDate(updateTicketForm.getStartTime(), updateTicketForm.getEndTime());
		TicketUpdate ticketUpdate = new TicketUpdate();
		ObjectUtil.copySimpleProperties(updateTicketForm, ticketUpdate);
		EntityUtil.initUpdateEntity(ticketUpdate);
		if (StringUtil.isEmpty(ticketUpdate.getReporter()) || StringUtil.isEmpty(ticketUpdate.getReporterName())) {
			ticketUpdate.setReporter(UserHolder.getUser().getCode());
			ticketUpdate.setReporterName(UserHolder.getUser().getCname());
		}
		ticketService.updateTickect(ticketUpdate);
		return BaseRestResult.refresh();
	}

	@RequestMapping(value = "/ticket/updatePlan")
	public BaseRestResult<String> updateTicketPan(UpdateTicketPlanForm updateTicketPlanForm) {
		TicketPlanUpdate ticketPlanUpdate = new TicketPlanUpdate();
		ObjectUtil.copySimpleProperties(updateTicketPlanForm, ticketPlanUpdate);
		EntityUtil.initUpdateEntity(ticketPlanUpdate);
		ticketService.updateTickectPlan(ticketPlanUpdate);
		return BaseRestResult.refresh();
	}

	@RequestMapping(value = "/ticket/changeStatus")
	public BaseRestResult<String> changeStatus(ChangeStatusForm changeStatusForm) {
		TicketChangeStatusVo ticketChangeStatusVo = new TicketChangeStatusVo();
		ticketChangeStatusVo.setTicketId(changeStatusForm.getTicketId());
		ticketChangeStatusVo.setTicketTypeStatusId(changeStatusForm.getTicketTypeStatusId());
		ticketChangeStatusVo.setComments(changeStatusForm.getComments());
		ticketChangeStatusVo.setOperator(changeStatusForm.getOperator());
		this.ticketService.changeStatus(ticketChangeStatusVo);
		return BaseRestResult.refresh();
	}

	@RequestMapping(value = "/ticket/getChangeStatusView")
	public HtmlRestResult getChangeStatusView(GetChangeStatusViewForm getChangeStatusViewForm) {
		TicketTypeStatus ticketTypeStatus = ticketTypeStatusService.getTicketTypeStatusById(getChangeStatusViewForm.getTicketTypeStatusId());
		Ticket ticket = this.ticketService.getTicketById(getChangeStatusViewForm.getTicketId());
		ProjectCommentTemplate projectCommentTemplate = projectCommentTemplateService.findProjectCommentTemplate(ticket.getProjectId(), ticket.getTicketTypeCode(), ticketTypeStatus.getNextStatus(), ticket.getModuleId());
		if (null != projectCommentTemplate) {
			ticketTypeStatus.setCommentTemplate(projectCommentTemplate.getCommentTemplate());
		}
		AssertInfoUtils.assertNotNull(ticketTypeStatus, "状态已经变更，无法执行该操作");
		ViewContext.put("ticket", ticketService.getTicketById(getChangeStatusViewForm.getTicketId()));
		ViewContext.put("ticketTypeStatus", ticketTypeStatus);
		ViewContext.put("ticketId", getChangeStatusViewForm.getTicketId());
		return HtmlRestResult.popHtmlFragment("/ticket/getChangeStatusView");
	}

	@RequestMapping(value = "/ticket/getAssignView")
	public HtmlRestResult getAssignView(GetAssignViewForm getAssignViewForm) {
		Ticket ticket = this.ticketService.getTicketById(getAssignViewForm.getTicketId());
		ViewContext.put("ticket", ticket);
		return HtmlRestResult.popHtmlFragment("/ticket/getAssignView");
	}

	@RequestMapping(value = "/ticket/changeOperator")
	public BaseRestResult<String> changeOperator(ChangeOperatorForm changeOperatorForm) {
		TicketUpdateOperatorVo ticketUpdateOperatorVo = new TicketUpdateOperatorVo();
		ticketUpdateOperatorVo.setTicketId(changeOperatorForm.getTicketId());
		ticketUpdateOperatorVo.setComments(changeOperatorForm.getComments());
		ticketUpdateOperatorVo.setOperator(changeOperatorForm.getOperator());
		this.ticketService.updateTicketOperator(ticketUpdateOperatorVo);
		return BaseRestResult.refresh();
	}

	@RequestMapping(value = "/ticket/autocomplete")
	public BaseRestResult<List<Object[]>> autocomplete(AutocompleteForm autocompleteForm) {
		List<Object[]> data = null;
		if (StringUtil.hasText(autocompleteForm.getKey())) {
			data = this.ticketService.getTicketsByKey(autocompleteForm.getKey());
		} else {
			data = new ArrayList<>();
		}
		return BaseRestResult.buildRestResult(data);
	}

	@RequestMapping(value = "/ticket/getTicketDescriptionsUpdateView")
	public HtmlRestResult getTicketDescriptionsUpdateView(TicketIdForm ticketIdForm) {
		Ticket ticket = this.ticketService.getTicketById(ticketIdForm.getTicketId());
		ViewContext.put("ticket", ticket);
		return HtmlRestResult.popHtmlFragment("/ticket/getTicketDescriptionsUpdateView");
	}

	@RequestMapping(value = "/ticket/updateTicketDescriptions")
	public BaseRestResult<String> updateTicketDescriptions(TicketDescriptionsUpdateForm ticketDescriptionsUpdateForm) {
		TicketDescriptionsUpdate ticketDescriptionsUpdate = new TicketDescriptionsUpdate();
		ObjectUtil.copySimpleProperties(ticketDescriptionsUpdateForm, ticketDescriptionsUpdate);
		EntityUtil.initUpdateEntity(ticketDescriptionsUpdate);
		this.ticketService.updateTicketDescriptions(ticketDescriptionsUpdate);
		return BaseRestResult.refresh();
	}

	@RequestMapping(value = "/ticket/initTicketDueDate")
	public BaseRestResult<String> initTicketDueDate() {
		List<String> ids = this.ticketService.getAllTicketIds();
		for (String ticketId : ids) {
			this.ticketService.updateTicketDueDate(ticketId);
		}
		return new BaseRestResult<String>("update due date row:" + ids.size());
	}

}
